package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Java {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://juggernaut.stadi.sonera.fi:10000/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testJava() throws Exception {
    // ERROR: Caught exception [unknown command [while]]
    driver.get(baseUrl + "/group/crm/homepage");
    driver.findElement(By.xpath("//div[@id='navigation']/ul/li[2]/a/span")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.cssSelector("span.action-icon.action-new-account"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.cssSelector("span.action-icon.action-new-account")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.id("_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id13_customForm_WAR_CustomForm_INSTANCE_nFj2_"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    // ERROR: Caught exception [ERROR: Unsupported command [getEval |  | ]]
    new Select(driver.findElement(By.id("_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id277_customForm_WAR_CustomForm_INSTANCE_nFj2_"))).selectByVisibleText("Physical");
    driver.findElement(By.id("_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id259_customForm_WAR_CustomForm_INSTANCE_nFj2_")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (isElementPresent(By.id("_idJsp2_CustomTable_WAR_CustomTable_INSTANCE_IyB8_:dataScroller_CustomTable_WAR_CustomTable_INSTANCE_IyB8_:1:_id3_CustomTable_WAR_CustomTable_INSTANCE_IyB8_"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.id("_idJsp2_CustomTable_WAR_CustomTable_INSTANCE_IyB8_:dataScroller_CustomTable_WAR_CustomTable_INSTANCE_IyB8_:1:_id3_CustomTable_WAR_CustomTable_INSTANCE_IyB8_")).click();
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (!isElementPresent(By.cssSelector("div.loading-animation"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    new Select(driver.findElement(By.id("_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id476_customForm_WAR_CustomForm_INSTANCE_nFj2_"))).selectByVisibleText("Paper invoice");
    for (int second = 0;; second++) {
    	if (second >= 60) fail("timeout");
    	try { if (!isElementPresent(By.cssSelector("div.loading-animation"))) break; } catch (Exception e) {}
    	Thread.sleep(1000);
    }

    driver.findElement(By.id("_idJsp3_customForm_WAR_CustomForm_INSTANCE_nFj2_:_id535_customForm_WAR_CustomForm_INSTANCE_nFj2_")).click();
    System.out.println("");
    // ERROR: Caught exception [unknown command [endWhile]]
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
